package cn.dapeng.sort;

import cn.dapeng.utils.BuilderArray;

/**
 * 直接插入排序
 */
public class InsertSort {

    /**
     * 野路子
     * @param array
     */
    public static void sort2(int[] array) {
        if (array == null || array.length < 2) return;
        int size = array.length;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < i + 1; j++) {
                if (array[i] < array[j]) {
                    BuilderArray.swap(array, i, j);
                }
            }
        }
    }

    public static void sort0(int[] array) {
        if (array == null || array.length < 2) return;
        int size = array.length;
        for (int end = 1; end < size; end++) {
            while (end - 1 >= 0 && array[end] < array[end - 1]) {
                BuilderArray.swap(array, end, end - 1);
                end--;
            }
        }
    }

    public static void sort(int[] array) {
        if (array == null || array.length < 2) return;
        int size = array.length;
        for (int end = 1; end < size; end++) {
            for (int pre = end - 1; pre >= 0 && array[pre] > array[pre + 1]; pre--) {
                BuilderArray.swap(array, pre, pre + 1);
            }
        }
    }

}
